package com.my.easymall.web;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @auther Summerday
 *
 * 注销用户登录状态
 */
@WebServlet("/LogOutServlet")
public class LogOutServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //将session删除,删除session后,其中属性值销毁,实现用户注销的功能

        //判断session对象是否为空,如果不为空,则不需要释放

        //代码已修改
        if(request.getSession(false)!=null){
            request.getSession().invalidate();
        }

        //30天内自动登录清除cookie,避免点击之后重复登录,需要等待30天
        Cookie cookie = new Cookie("autologin","");
        cookie.setMaxAge(0);
        cookie.setPath(request.getContextPath()+"/");
        response.addCookie(cookie);

        response.sendRedirect("http://www.easymall.com/");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
